"use strict";
var ControlEvents = function(camProp,mark,mouseDownCallback,mouseMoveCallback,jumpCallback) {
    this.mouse = {};
    this.downFuc = null;
    this.prop = camProp;
    this.markingPos = mark;
    this.moveCallback = mouseMoveCallback;
    this.downCallback = mouseDownCallback;
    this.jumpCallback = jumpCallback;
};

ControlEvents.prototype = {
    
    attachControlEvents: function(container) {
        container.addEventListener('mousewheel', this.onMouseWheel.bind(this), false);
        container.addEventListener("mousedown", this.onMouseDown.bind(this), false);
        container.addEventListener('mouseup', this.onMouseUp.bind(this),false);
        container.addEventListener('mousemove',this.onMouseMove.bind(this),false); 
    },

    onMouseUp: function(event) {
        clearInterval(this.downFuc);
        if (event.clientX != this.mouse.x || event.clientY != this.mouse.y) {
            console.log('slide');
            
        } else {
            console.log('click');
            
        }
        this.markingPos.selectedIndex = -1;
    },
   
    onMouseDown: function(event) { 
        this.mouse = {y:event.clientY,x:event.clientX};

        if (this.jumpCallback) {
            this.jumpCallback(event);
        }
        this.downFuc = setInterval(function() {
          this.onMouseUp(event);
          if (this.downCallback) {
              this.downCallback(event);
          }

        }.bind(this),1000);
    },

    

    onMouseMove: function(event) {
        clearInterval(this.downFuc);

        if (this.moveCallback) {
            this.moveCallback(event);
        }
        
    },

    onMouseWheel: function(e) {

        e.preventDefault();
        if (e.wheelDelta) {  //判断浏览器IE，谷歌滑轮事件
            if (e.wheelDelta > 0) { //当滑轮向上滚动时
              this.prop.fov -= (this.prop.near < this.prop.fov ? 1 : 0);
            }
            if (e.wheelDelta < 0) { //当滑轮向下滚动时
              this.prop.fov += (this.prop.fov < this.prop.far ? 1 : 0);
            }
        } else if (e.detail) {  //Firefox滑轮事件
            if (e.detail > 0) { //当滑轮向上滚动时
              this.prop.fov -= 1;
            }
            if (e.detail < 0) { //当滑轮向下滚动时
              this.prop.fov += 1;
            }
        }

        
        if(this.prop.fov < this.prop.fovMin) {
          this.prop.fov = this.prop.fovMin;
        } else if(this.prop.fov > this.prop.fovMax) {
          this.prop.fov = this.prop.fovMax;
        }
        //改变fov值，并更新场景的渲染
        this.prop.camera.fov = this.prop.fov;
        this.prop.camera.updateProjectionMatrix();
    },
};